package homework;

import com.sun.source.doctree.ThrowsTree;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class FightLandlord extends JFrame {
    private List<Card> poker = new ArrayList<>();

    private List<Card> player1 = new ArrayList<>();
    private List<Card> player2 = new ArrayList<>();
    private List<Card> player3 = new ArrayList<>();
    private List<Card> bottom = new ArrayList<>();


//    Icon pic = Util.getIcon("/cards/" + (i + 2) + "_" + j + ".jpg");

//    private Icon pic = Util.getIcon("/cards/3_1.jpg");
    
    public FightLandlord() {
        this.init();
        this.xipai();
        this.fapai();
        //设置空布局
        this.setLayout(null);
        for (int i = 16; i >= 0; i--) {
            Icon pic1 = Util.getIcon(player1.get(i).getPic());
            JLabel imageLabel1 = new JLabel(pic1);
            imageLabel1.setBounds(10, 50 + (30 * i), 150, 150);
            this.add(imageLabel1);
        }
        for (int i = 16; i >= 0; i--) {
            Icon pic2 = Util.getIcon(player2.get(i).getPic());
            JLabel imageLabel2 = new JLabel(pic2);
            imageLabel2.setBounds(300 + (40 * i), 600, 150, 150);
            this.add(imageLabel2);
        }
        for (int i = 16; i >= 0; i--) {
            Icon pic3 = Util.getIcon(player3.get(i).getPic());
            JLabel imageLabel3 = new JLabel(pic3);
            imageLabel3.setBounds(1200, 50 + (30 * i), 150, 150);
            this.add(imageLabel3);
        }
        for (int i = 2; i >= 0; i--) {
            Icon pic4 = Util.getIcon(bottom.get(i).getPic());
            JLabel imageLabel4 = new JLabel(pic4);
            imageLabel4.setBounds(550 + (40 * i), 50, 150, 150);
            this.add(imageLabel4);
        }
        JButton button1 = new JButton("开始");
        button1.setBounds(600, 300, 100, 100);
        this.add(button1);


        //设置窗口大小(先宽后高)
        this.setSize(1400, 900);

        //设置左上角图标

        //设置窗体居中，写在窗口大小后面
        this.setLocationRelativeTo(null);

        //设置窗体的标题文字，传String类型
        this.setTitle("测试窗口");

        //设置关闭时程序结束，传int类型，java自带常量设定
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        //设置窗体可见,写在最后面,传布尔值
        this.setVisible(true);

    }


    // 初始化54张扑克牌
    public void init() {
        String[] points = "3,4,5,6,7,8,9,10,J,Q,K,A,2".split(",");
        String[] types = "♠,♥,♣,♦".split(",");
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < types.length; j++) {
                poker.add(new Card(points[i], types[j], i, j, "/cards/" + (i + 3) + "_" + (j + 1) + ".jpg"));
            }
        }
        poker.add(new Card("大王", "", 14, 0, "/cards/17_1.jpg"));
        poker.add(new Card("小王", "", 13, 0, "/cards/16_1.jpg"));

        System.out.println(poker);
    }

    // 洗牌
    public void xipai() {
        Collections.shuffle(poker);
        System.out.println(poker);
    }

    // 发牌
    public void fapai() {
        player1 = poker.subList(0, 17);
        player2 = poker.subList(17, 34);
        player3 = poker.subList(34, 51);
        bottom = poker.subList(51, 54);

        sort(player1);
        sort(player2);
        sort(player3);
        sort(bottom);
        System.out.println("玩家一：" + player1);
        System.out.println("玩家二：" + player2);
        System.out.println("玩家三：" + player3);
        System.out.println("底牌：" + bottom);
    }

    public void sort(List list) {
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getPointValue() == o2.getPointValue()) {
                    return o1.getTypeValue() - o2.getTypeValue();
                } else {
                    return o1.getPointValue() - o2.getPointValue();
                }
            }
        });
    }

    public static void main(String[] args) {
        FightLandlord dok = new FightLandlord();
        dok.init();
        dok.xipai();
        dok.fapai();
    }
}

